#include "YGDate.h"
#include <time.h>

using namespace YoghurtGum;

YG_TIMECONTAINER  Date::s_TimeCurrent;
char              Date::s_Output[YG_LOG_SIZE];

const char nameDays[][12] = 
{
	"Monday",
	"Tuesday",
	"Wednesday",
	"Thursday",
	"Friday",
	"Saturday",
	"Sunday"
};

const char nameMonths[][12] = 
{
	"January",
	"February",
	"March",
	"April",
	"May",
	"June",
	"July",
	"August",
	"September",
	"October",
	"November",
	"December"
};

void Date::AddNumber(int a_Number)
{
	/*int first, second;

	first = s_TimeCurrent.wMinute / 10;
	s_Output[write++] = (char)(first + 48);
	second = s_TimeCurrent.wMinute - (first * 10);
	s_Output[write++] = (char)(second + 48);*/
}



YG_TIMECONTAINER Date::Convert(time_t a_UnixTime)
{

#if (YG_OS_TYPE == YG_OS_TYPE_ANDROID)

	YG_TIMECONTAINER bleh;
	return bleh;

#elif (YG_OS_TYPE == YG_OS_TYPE_WINDOWSMOBILE)

	YG_TIMECONTAINER newtime;
	ZeroMemory(&newtime, sizeof(newtime));

	//time(&a_UnixTime);
	//struct tm* timeinfo = localtime(&a_UnixTime);

	//char buffer[1024];

	//YG_TIMECONTAINER systime;

	/*FILETIME what;
	SystemTimeToFileTime(&date_1970, &what);
	FileTimeToSystemTime(&what, &systime);*/

	newtime.wMilliseconds = 0;
	//newtime.wYear	= strftime(buffer, 1024, "%Y", timeinfo);
	//newtime.wDay	= strftime(buffer, 1024, "%d", timeinfo);

	// 302 day
	return newtime;

#endif

}

char* Date::Format(const char* a_Format, time_t a_UnixTime)
{

#if (YG_OS_TYPE == YG_OS_TYPE_ANDROID)
	
	// TODO: Convert format.
	strcpy(s_Output, "<not implemented>");

#elif (YG_OS_TYPE == YG_OS_TYPE_WINDOWSMOBILE)

	// empty output array
	memset(s_Output, 0, YG_LOG_SIZE * sizeof(char));

	// get time
	if (a_UnixTime == 0)
	{
		GetLocalTime(&s_TimeCurrent);
	}
	else
	{
		s_TimeCurrent = Convert(a_UnixTime);
	}

	char formatted[32];
	int write = 0;
	int read = 0;

	for (read = 0; a_Format[read]; ++read)
	{
		if (a_Format[read] == '$')
		{
			// time (hh:nn)
			if (
				a_Format[read + 1] == 't' && 
				a_Format[read + 2] == 'i' && 
				a_Format[read + 3] == 'm' && 
				a_Format[read + 4] == 'e'
			)
			{
				int first, second;

				first = s_TimeCurrent.wHour / 10;
				s_Output[write++] = (char)(first + 48);
				second = s_TimeCurrent.wHour - (first * 10);
				s_Output[write++] = (char)(second + 48);

				s_Output[write++] = ':';

				first = s_TimeCurrent.wMinute / 10;
				s_Output[write++] = (char)(first + 48);
				second = s_TimeCurrent.wMinute - (first * 10);
				s_Output[write++] = (char)(second + 48);

				read += 4;
			}

			// time (hh:nn)
			else if 
			(
				a_Format[read + 1] == 'T' && 
				a_Format[read + 2] == 'I' && 
				a_Format[read + 3] == 'M' && 
				a_Format[read + 4] == 'E'
			)
			{
				sprintf(formatted, "%02i:%02i:%02i", s_TimeCurrent.wHour, s_TimeCurrent.wMinute, s_TimeCurrent.wSecond);
				strncat(s_Output, formatted, 8);

				read += 4;
			}

			// clock (HH:nn AM/PM)
			else if 
			(
				a_Format[read + 1] == 'c' && 
				a_Format[read + 2] == 'l' && 
				a_Format[read + 3] == 'o' && 
				a_Format[read + 4] == 'c' &&
				a_Format[read + 5] == 'k'
			)
			{
				int hour = s_TimeCurrent.wHour;
				if (hour > 12) 
				{ 
					hour -= 12; 
					sprintf(formatted, "%02i:%02i PM", hour, s_TimeCurrent.wMinute);
				}
				else
				{
					sprintf(formatted, "%02i:%02i AM", hour, s_TimeCurrent.wMinute);
				}

				strncat(s_Output, formatted, 8);

				read += 5;
			}

			// CLOCK (HH:nn:ss AM/PM)
			else if 
			(
				a_Format[read + 1] == 'C' && 
				a_Format[read + 2] == 'L' && 
				a_Format[read + 3] == 'O' && 
				a_Format[read + 4] == 'C' &&
				a_Format[read + 5] == 'K'
			)
			{
				int hour = s_TimeCurrent.wHour;
				if (hour > 12) 
				{ 
					hour -= 12; 
					sprintf(formatted, "%02i:%02i:%02i PM", hour, s_TimeCurrent.wMinute, s_TimeCurrent.wSecond);
				}
				else
				{
					sprintf(formatted, "%02i:%02i:%02i AM", hour, s_TimeCurrent.wMinute, s_TimeCurrent.wSecond);
				}

				strncat(s_Output, formatted, 11);

				read += 5;
			}

			// iso (yyyy-mm-dd)
			else if 
			(
				a_Format[read + 1] == 'i' && 
				a_Format[read + 2] == 's' && 
				a_Format[read + 3] == 'o'
			)
			{
				sprintf(formatted, "%i-%02i-%02i", s_TimeCurrent.wYear, s_TimeCurrent.wMonth, s_TimeCurrent.wDay);
				strncat(s_Output, formatted, 10);

				read += 3;
			}

			// le (dd-mm-yyyy)
			else if 
			(
				a_Format[read + 1] == 'l' && 
				a_Format[read + 2] == 'e'
			)
			{
				sprintf(formatted, "%02i-%02i-%i", s_TimeCurrent.wDay, s_TimeCurrent.wMonth, s_TimeCurrent.wYear);
				strncat(s_Output, formatted, 10);

				read += 2;
			}

			// be (yyyy-MM-dd)
			else if 
			(
				a_Format[read + 1] == 'b' && 
				a_Format[read + 2] == 'e'
			)
			{
				sprintf(formatted, "%i-%s-%02i", s_TimeCurrent.wYear, nameMonths[s_TimeCurrent.wMonth - 1], s_TimeCurrent.wDay);
				int length = strlen(formatted);
				strncat(s_Output, formatted, length);

				read += 2;
			}

			// me (mm/dd/yyyy)
			else if 
			(
				a_Format[read + 1] == 'm' && 
				a_Format[read + 2] == 'e'
			)
			{
				sprintf(formatted, "%02i/%02i/%i", s_TimeCurrent.wMonth, s_TimeCurrent.wDay, s_TimeCurrent.wYear);
				strncat(s_Output, formatted, 10);

				read += 2;
			}

			// 24 hour
			else if (a_Format[read + 1] == 'h')
			{
				if (a_Format[read + 2] == 'h')
				{
					sprintf(formatted, "%02i", s_TimeCurrent.wHour);
					strncat(s_Output, formatted, 2);

					read += 2;
				}
				else
				{
					sprintf(formatted, "%i", s_TimeCurrent.wHour);
					int length = strlen(formatted);
					strncat(s_Output, formatted, length);

					read += 1;
				}
			}

			// 12 hour
			else if (a_Format[read + 1] == 'H')
			{
				int hour = s_TimeCurrent.wHour;
				if (hour > 12) { hour -= 12; }

				if (a_Format[read + 2] == 'H')
				{
					sprintf(formatted, "%02i", hour);
					strncat(s_Output, formatted, 2);

					read += 2;
				}
				else
				{
					sprintf(formatted, "%i", hour);
					int length = strlen(formatted);
					strncat(s_Output, formatted, length);

					read += 1;
				}
			}

			// minutes
			else if (a_Format[read + 1] == 'n')
			{
				if (a_Format[read + 2] == 'n')
				{
					sprintf(formatted, "%02i", s_TimeCurrent.wMinute);
					strncat(s_Output, formatted, 2);

					read += 2;
				}
				else
				{
					sprintf(formatted, "%i", s_TimeCurrent.wMinute);
					int length = strlen(formatted);
					strncat(s_Output, formatted, length);

					read += 1;
				}
			}

			// seconds
			else if (a_Format[read + 1] == 's')
			{
				if (a_Format[read + 2] == 's')
				{
					sprintf(formatted, "%02i", s_TimeCurrent.wSecond);
					strncat(s_Output, formatted, 2);

					read += 2;
				}
				else
				{
					sprintf(formatted, "%i", s_TimeCurrent.wSecond);
					int length = strlen(formatted);
					strncat(s_Output, formatted, length);

					read += 1;
				}
			}

			// day
			else if (a_Format[read + 1] == 'd')
			{
				if (a_Format[read + 2] == 'd')
				{
					sprintf(formatted, "%02i", s_TimeCurrent.wDay);
					strncat(s_Output, formatted, 2);

					read += 2;
				}
				else
				{
					sprintf(formatted, "%i", s_TimeCurrent.wDay);
					int length = strlen(formatted);
					strncat(s_Output, formatted, length);

					read += 1;
				}
			}

			// month
			else if (a_Format[read + 1] == 'm')
			{
				if (a_Format[read + 2] == 'm')
				{
					sprintf(formatted, "%02i", s_TimeCurrent.wMonth);
					strncat(s_Output, formatted, 2);

					read += 2;
				}
				else
				{
					sprintf(formatted, "%i", s_TimeCurrent.wMonth);
					int length = strlen(formatted);
					strncat(s_Output, formatted, length);

					read += 1;
				}
			}

			// month (name)
			else if (a_Format[read + 1] == 'M')
			{
				sprintf(formatted, "%s", nameMonths[s_TimeCurrent.wMonth - 1]);

				if (a_Format[read + 2] == 'M')
				{
					// long name
					int length = strlen(formatted);
					strncat(s_Output, formatted, length);

					read += 2;
				}
				else
				{
					// shortened name
					strncat(s_Output, formatted, 3);

					read += 1;
				}
			}

			// year
			else if (a_Format[read + 1] == 'y' && a_Format[read + 2] == 'y')
			{
				sprintf(formatted, "%i", s_TimeCurrent.wYear);

				if (a_Format[read + 3] == 'y' && a_Format[read + 4] == 'y')
				{
					strncat(s_Output, formatted, 4);
					
					read += 4;
				}
				else
				{
					strncat(s_Output, formatted + 2 * sizeof(char), 2);

					read += 2;
				}
			}
			else
			{
				strncat(s_Output, &a_Format[read], 1);
			}
		}
		else
		{
			strncat(s_Output, &a_Format[read], 1);
		}
	}

#endif

	return s_Output;
}
